using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutCustomerDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<OutCustomer> GetOutCustomerList()
        {
            try
            {
                const string spName = "spo_OutCustomer_GetList";

                var OutCustomerList = DbHelper.ExecuteSPList<OutCustomer>(spName);
                return OutCustomerList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return new List<OutCustomer>();
            }
        }

        public List<OutCustomer> GetOutCustomerListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "spo_OutCustomer_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var OutCustomerList = DbHelper.ExecuteSqlList<OutCustomer>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return OutCustomerList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return new List<OutCustomer>();
            }
        }

        public OutCustomer GetOutCustomerById(int cusId)
        {
            try
            {
                const string spName = "spo_OutCustomer_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@CusId", cusId)
                                    };

                var OutCustomer = DbHelper.ExecuteSPList<OutCustomer>(spName, sqlParams).SingleOrDefault();
                return OutCustomer;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return null;
            }
        }

        public int InsertOutCustomer(OutCustomer OutCustomer)
        {
            try
            {
                const string spName = "spo_OutCustomer_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@CusUserName", OutCustomer.CusUserName),
										new SqlParameter("@CusPass", OutCustomer.CusPass),
										new SqlParameter("@CusEmail", OutCustomer.CusEmail),
										new SqlParameter("@CusPhone", OutCustomer.CusPhone),
										new SqlParameter("@CusFullName", OutCustomer.CusFullName),
										new SqlParameter("@CusAddress", OutCustomer.CusAddress),
										new SqlParameter("@CusImg", OutCustomer.CusImg),
										new SqlParameter("@CusIdCard", OutCustomer.CusIdCard),
										new SqlParameter("@CusStatus", OutCustomer.CusStatus),
										new SqlParameter("@CusId", OutCustomer.CusId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@CusId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@CusId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@CusId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int UpdateOutCustomer(OutCustomer OutCustomer)
        {
            try
            {
                const string spName = "spo_OutCustomer_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@CusId", OutCustomer.CusId)
										, new SqlParameter("@CusUserName", OutCustomer.CusUserName)
										, new SqlParameter("@CusPass", OutCustomer.CusPass)
										, new SqlParameter("@CusEmail", OutCustomer.CusEmail)
										, new SqlParameter("@CusPhone", OutCustomer.CusPhone)
										, new SqlParameter("@CusFullName", OutCustomer.CusFullName)
										, new SqlParameter("@CusAddress", OutCustomer.CusAddress)
										, new SqlParameter("@CusImg", OutCustomer.CusImg)
										, new SqlParameter("@CusIdCard", OutCustomer.CusIdCard)
										, new SqlParameter("@CusStatus", OutCustomer.CusStatus)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int DeleteOutCustomer(int cusId)
        {
            try
            {
                const string spName = "spo_OutCustomer_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@CusId", cusId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
